package com.sykj.weichat.web;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sykj.weichat.service.BisProjectRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.weichat.model.BisBagProject;
import com.sykj.weichat.model.BisBagRecord;
import com.sykj.weichat.model.BisBusinessOrder;
import com.sykj.weichat.model.BisCustomUser;
import com.sykj.weichat.model.BisNurseBag;
import com.sykj.weichat.model.BisNurseProject;
import com.sykj.weichat.model.BisProjectRecord;
import com.sykj.common.WeChatConfig;
import com.sykj.util.ResultBody;
import com.sykj.util.SignUtil;
import com.sykj.weichat.service.BisBagProjectService;
import com.sykj.weichat.service.BisBagRecordService;
import com.sykj.weichat.service.BisBusinessOrderService;
import com.sykj.weichat.service.BisCustomUserService;
import com.sykj.weichat.service.BisNurseBagService;
import com.sykj.weichat.service.BisNurseProjectService;

/**
 * 
 * 类名:com.sykj.weichat.web.WechatBagController
 * 描述:微信礼包开发
 * @author 张孝柱
 * 创建时间:2018年9月6日 
 * 修改说明:
 */
@Controller
@RequestMapping("/weichat/bag")
public class WeichatBagController {
	/**
	 * appID
	 */ 
	 public static final String appid = "wx5fcf6284ca955e5f";
	@Autowired
	BisNurseBagService BisNurseBagService;
	@Autowired
    BisBagRecordService BisBagRecordService;
    @Autowired
    BisNurseProjectService BisNurseProjectService;
    @Autowired
    BisCustomUserService BisCustomUserService;
    @Autowired
    BisBusinessOrderService BisBusinessOrderService;
    @Autowired
    BisProjectRecordService BisProjectRecordService;
    @Autowired
    BisBagProjectService BisBagProjectService;
	@RequestMapping("/")
	public String bag() {
		return "weichat/bag/bagList.jsp";
	}
	@RequestMapping("/bagList")
	@ResponseBody
	public ResultBody bagList(String mobile,Model model) {
		List<BisNurseBag> getall = BisNurseBagService.getall();
		if (getall!=null) {
			model.addAttribute("bagList", getall);
		}
		return ResultBody.getData(getall.size(), getall, "success");


	}
	/**
	 * 
	 * 简要说明：跳转我的礼包
	 * 编写者：张孝柱
	 * 创建时间：2018年9月13日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("/goMyBag")
	public String goMyBag() {
		return "weichat/bag/myBag.jsp";
	}
	/**
	 * 
	 * 简要说明：抢礼包
	 * 编写者：张孝柱
	 * 创建时间：2018年9月13日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "listMyBag")
	@ResponseBody
	public ResultBody listMyBag(HttpSession session){
		String custom_user_id =  (String) session.getAttribute("custom_user_id");
		List<BisBagRecord> getall = BisBagRecordService.getall(custom_user_id);
		return ResultBody.getData(getall.size(), getall, "success");
	}
	/**
	 * 
	 * 简要说明：领礼包
	 * 编写者：张孝柱
	 * 创建时间：2018年9月13日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "receiveBag")
	@ResponseBody
	public String receiveBag(String record_id,String bag_id,BisBagRecord bagRecord,BisProjectRecord bisProjectRecord){
		List<BisBagRecord> userId = BisBagRecordService.getUserId(record_id);
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		Date day = new Date();
		if (userId!=null) {
			BisBagRecordService.update(bagRecord);
			List<BisBagProject> getusers = BisBagProjectService.getusers(bag_id);
			for (BisBagProject bisBagProject : getusers) {
				String project_id = bisBagProject.getProject_id();
				bisProjectRecord.setProject_id(project_id);
				bisProjectRecord.setBag_record_id(bag_id);
				bisProjectRecord.setDraw_time(day);
				BisProjectRecordService.add(bisProjectRecord);
			}
			result = String.format(result,"1", "领取成功");
		} else {
			result = String.format(result,"0", "领取失败");
		}
		return result;
		
	}
	/**
	 * 查看礼包详情
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "showBagDetail")
	public String showBagDetail(String bag_id,Model model){
		BisNurseBag bag = BisNurseBagService.bag(bag_id);
		List<BisNurseProject> listBagProject = BisNurseProjectService.listBagProject(bag_id);
		double oldTotalPrice=0;
		double newTotalPrice=0;
		String bagProject="";
		for(int i=0;i<listBagProject.size();i++){
			BisNurseProject bisNurseProject = listBagProject.get(i);
			String project_name=bisNurseProject.getProject_name();
			int project_num=bisNurseProject.getProject_num();
			oldTotalPrice+=bisNurseProject.getRmb_price()*project_num;
			newTotalPrice+=bisNurseProject.getProject_new_price()*project_num;
			if(i==0){
				bagProject+=project_name;	
			}else{
				bagProject+="+"+project_name;
			}
		}
		double remainTotalPrice=oldTotalPrice-newTotalPrice;
		model.addAttribute("bagProject", bagProject);
		model.addAttribute("remainTotalPrice", remainTotalPrice);
		model.addAttribute("bag", bag);
		model.addAttribute("bagProjectList", listBagProject);
		return "weichat/bag/showBagDetail.jsp";
		
	}
	/**
	 * 
	 * 简要说明：判断是否抢过礼包
	 * 编写者：张孝柱
	 * 创建时间：2018年9月19日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "checkRobBag")
	@ResponseBody
	public String checkRobBag(String bag_id,HttpSession session,BisBagRecord bagRecord){
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		bagRecord.setCustom_user_id(custom_user_id);
		bagRecord.setBag_id(bag_id);
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\" }";
		BisBagRecord checkRobBag = BisBagRecordService.checkRobBag(bagRecord);
		  
		if (checkRobBag!=null) {
			result = String.format(result,"0", "您已抢过礼包，不能在次购买！");
		}else {
			result = String.format(result,"1", "Success！");
		}
		return result;
		
	}
	/**
	 * 
	 * 简要说明：抢礼包
	 * 编写者：张孝柱
	 * 创建时间：2018年9月19日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "goRobBag")
	public String goRobBag(String bag_id,Model model){
		BisNurseBag bag = BisNurseBagService.bag(bag_id);
		model.addAttribute("bag", bag);
		return "weichat/bag/buyBag.jsp";
	}
	/**
	 * 
	 * 简要说明：获取微信配置信息
	 * 编写者：张孝柱
	 * 创建时间：2018年10月8日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "getWechatConfig")
	@ResponseBody
	public String getWechatConfig(String url,HttpServletRequest request, HttpServletResponse response,HttpSession session){
		String result = "{\"appId\":\"%s\", \"timestamp\":\"%s\", \"nonceStr\":\"%s\", \"signature\":\"%s\",\"appcode\":\"%s\", \"appmsg\":\"%s\"  }";
		String signature = WeChatConfig.WECHAT_CONFIG.get("signature");
		String timestamp = WeChatConfig.WECHAT_CONFIG.get("timestamp");
		String nonce = WeChatConfig.WECHAT_CONFIG.get("nonce");
		String echostr = WeChatConfig.WECHAT_CONFIG.get("echostr");
		System.out.println("===signature=" + signature + "==timestamp="+timestamp+"===nonce=="+nonce+"==echostr=="+echostr);
		result = String.format(result,appid,signature,nonce,session,1,"成功分享");
		return result;
	}
}
